package com.siemens.industrial.web.mvc.controller.partner.user;

import com.siemens.industrial.utils.ExportExcel;
import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.web.mvc.service.admin.TenantService;
import com.siemens.industrial.web.mvc.service.partner.PartnerChartsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shuan on 2016/11/15.
 */
@Controller
public class PartnerChartsController {
    @Resource
    PartnerChartsService partnerChartsService;

    @Resource
    TenantService tenantService;

    @RequestMapping(value = "/partner/{tenant}/chart/sameDeviceDiffReason", method = RequestMethod.GET)
    public String getSameDeviceDiffReason(@PathVariable("tenant") String tenantKey,
                                          ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        modelMap.put("key", tenantId);
        return "/partner/user/charts/sameDeviceDiffReasonChart";
    }

    @RequestMapping(value = "/partner/{tenant}/chart/sameDeviceSameReason", method = RequestMethod.GET)
    public String getSameDeviceSameReason(@PathVariable("tenant") String tenantKey,
                                          ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        modelMap.put("key", tenantId);
        return "/partner/user/charts/sameDeviceSameReasonChart";
    }

    @RequestMapping(value = "/partner/{tenant}/chart/sameIdleReasonDiffDevice", method = RequestMethod.GET)
    public String getSameIdleReasonDiffDevice(@PathVariable("tenant") String tenantKey,
                                              ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        modelMap.put("key", tenantId);
        return "/partner/user/charts/sameIdleReasonDiffDeviceChart";
    }

    @RequestMapping(value = "/partner/{tenant}/chart/shiftIdle", method = RequestMethod.GET)
    public String getShiftIdle(@PathVariable("tenant") String tenantKey,
                               ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        modelMap.put("key", tenantId);
        return "/partner/user/charts/shiftIdlePieChart";
    }

    @RequestMapping(value = "/partner/{tenant}/chart/dayOrShiftStateChart", method = RequestMethod.GET)
    public String getDayOrShiftState(@PathVariable(value = "tenant") String tenantKey,
                                     ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        modelMap.put("key", tenantId);
        return "/partner/user/charts/dayOrShiftStatePieChart";
    }

    @RequestMapping(value = "/partner/{tenant}/chart/deviceServiceChart", method = RequestMethod.GET)
    public String getDeviceService(@PathVariable("tenant") String tenantKey,
                                   ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        modelMap.put("key", tenantId);
        return "/partner/user/charts/deviceServiceChart";
    }

    @RequestMapping(value = "/partner/{tenant}/chart/shiftDeviceServiceChart", method = RequestMethod.GET)
    public String getShiftDeviceService(@PathVariable("tenant") String tenantKey,
                                        ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        modelMap.put("key", tenantId);
        return "/partner/user/charts/shiftDeviceServiceChart";
    }

    @RequestMapping(value = "/partner/{tenant}/chart/deviceStateBarChart", method = RequestMethod.GET)
    public String getDeviceStateBarChart(@PathVariable("tenant") String tenantKey,
                                         ModelMap modelMap) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        modelMap.put("key", tenantId);
        return "/partner/user/charts/deviceStateBarChart";
    }

    //获取数据：同机床不同原因折线图
    @RequestMapping(value = "/partner/{tenant}/api/v1/getSameDeviceDiffReason", method = RequestMethod.GET)
    @ResponseBody
    public Map getSameDeviceDiffTypeTimeAndAllType(@PathVariable("tenant") String tenantKey,
                                                   @RequestParam(value = "device") String device,
                                                   @RequestParam(value = "startTime") String startTime,
                                                   @RequestParam(value = "endTime") String endTime) {
        try {
            String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
            Map map = partnerChartsService.getSameDeviceDiffTypeTime(tenantId, device, startTime, endTime);

            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
        } catch (Exception e) {
            return null;
        }
    }

    //获取数据：同机床同原因折线图
    @RequestMapping(value = "/partner/{tenant}/api/v1/getSameDeviceSameReason", method = RequestMethod.GET)
    @ResponseBody
    public Map getSameDeviceSameTypeTime(@PathVariable("tenant") String tenantKey,
                                         @RequestParam(value = "device") String device,
                                         @RequestParam(value = "reason") String reason,
                                         @RequestParam(value = "reasonName") String reasonName,
                                         @RequestParam(value = "startTime") String startTime,
                                         @RequestParam(value = "endTime") String endTime) {
        try {
            String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
            Map map = partnerChartsService.getSameDeviceSameTypeTime(tenantId, device, reason, reasonName, startTime, endTime);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //获取数据：同原因全部机床折线图
    @RequestMapping(value = "/partner/{tenant}/api/v1/getSameReasonDevices", method = RequestMethod.GET)
    @ResponseBody
    public Map getSameReasonDevices(@PathVariable("tenant") String tenantKey,
                                    @RequestParam(value = "reason") String reason,
                                    @RequestParam(value = "reasonName") String reasonName,
                                    @RequestParam(value = "startTime") String startTime,
                                    @RequestParam(value = "endTime") String endTime) {
        try {
            String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
            Map map = partnerChartsService.getSameReasonDevices(tenantId, reason, reasonName, startTime, endTime);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
        } catch (Exception e) {
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //24小时不同班次空闲原因总体分布
    @RequestMapping(value = "/partner/{tenant}/api/v1/dayShiftFreeReasonTotal", method = RequestMethod.GET)
    @ResponseBody
    public Map dayShiftFreeReasonTotal(@PathVariable("tenant") String tenantKey,
                                       @RequestParam(value = "startTime") String startTime,
                                       @RequestParam(value = "endTime") String endTime,
                                       @RequestParam(value = "day") String day,
                                       @RequestParam(value = "deviceId") String deviceId,
                                       @RequestParam(value = "deviceName") String deviceName) {
        try {
            String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();

            Map map = partnerChartsService.getShiftDeviceFreeInfo(tenantId, day, startTime, endTime, deviceId);

            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //获取全部的idleReason
    @RequestMapping(value = "/partner/{tenant}/api/v1/getIdleReasonList", method = RequestMethod.GET)
    @ResponseBody
    public Map getIdleReasonList(@PathVariable("tenant") String tenantKey) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();

        List idleReason = partnerChartsService.getIdleReasons(tenantId);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(idleReason).build();
    }

    //获取全部故障
    @RequestMapping(value = "/partner/{tenant}/api/v1/getFaultType", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getFaultType(@PathVariable("tenant") String tenantKey) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();

        List faultType = partnerChartsService.getFaultType(tenantId);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(faultType).build();
    }

    //获取某班次 performance availability quality oee数据
    @RequestMapping(value = "/partner/{tenant}/api/v1/product/shiftService", method = RequestMethod.GET)
    @ResponseBody
    public Map getServiceByShift(@PathVariable("tenant") String tenantKey,
                                 @RequestParam(value = "day") String day,
                                 @RequestParam(value = "deviceIdArr[]") String[] deviceIdArr,
                                 @RequestParam(value = "type") String type) {
        try {
            String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();

            List<Map<String,Object>> map = partnerChartsService.serviceShiftCompareNew(tenantId, deviceIdArr, day, type);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //获取所有机床根据机床类型和机床型号
    @RequestMapping(value = "/partner/{tenant}/api/v1/getDevicesByTypeAndModel", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDevicesByModelAndType(@PathVariable("tenant") String tenantKey,
                                                        @RequestParam(value = "type") String type,
                                                        @RequestParam(value = "model") String model) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List list = partnerChartsService.getDevicesByModelAndType(tenantId, type, model);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
    }

    //oee 对比 一段时间(原 可选)
    @RequestMapping(value = "/partner/{tenant}/api/v1/serviceContrast", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getServiceList(@PathVariable("tenant") String tenantKey,
                                              @RequestParam(value = "deviceIdArr[]") String[] deviceIdArr,
                                              @RequestParam(value = "startTime") String startTime,
                                              @RequestParam(value = "endTime") String endTime) {
        try {
            String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
            List<Map<String, Object>> serviceList = partnerChartsService.serviceCompare(tenantId, deviceIdArr, startTime, endTime);
//            List<Map<String, Object>> serviceList = partnerChartsService.serviceCompareNew(tenantId, deviceIdArr, startTime, endTime);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(serviceList).build();
        } catch (ParseException e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(new HashMap<>()).build();
        }
    }

    //oee 对比 一段时间(oee=availability)
    @RequestMapping(value = "/partner/{tenant}/api/v1/serviceContrastNew", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getServiceListNew(@PathVariable("tenant") String tenantKey,
                                                 @RequestParam(value = "deviceIdArr[]") String[] deviceIdArr,
                                                 @RequestParam(value = "startTime") String startTime,
                                                 @RequestParam(value = "endTime") String endTime,
                                                 @RequestParam(value = "type") String type) {
        try {
            String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
            Map<String, Object> serviceList = partnerChartsService.serviceCompareNew(tenantId, deviceIdArr, startTime, endTime,type);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(serviceList).build();
        } catch (ParseException e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(new HashMap<>()).build();
        }
    }

    //获取设备发生过的idle原因
    @RequestMapping(value = "/partner/{tenant}/api/v1/getIdleReasonByDevice", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getIdleReasonByDevice(@PathVariable("tenant") String tenantKey,
                                                     @RequestParam(value = "deviceId") String deviceId) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String, Object>> list = partnerChartsService.getIdleReasonByDevice(tenantId, deviceId);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
    }

    //获取：最近的系统性能参数
    @RequestMapping(value = "/partner/{tenant}/api/v1/recentlyIndexParam", method = RequestMethod.GET)
    @ResponseBody
    public Map getRecentlyIndexParam(@PathVariable("tenant") String tenantKey,
                                     @RequestParam(value = "deviceId") String deviceId,
                                     @RequestParam(value = "day")String day) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        Map map = partnerChartsService.getRecentlyIndexParam(tenantId, deviceId,day);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
    }

    //获取：机床某天工作状态
    @RequestMapping(value = "/partner/{tenant}/api/v1/workStatusPie", method = RequestMethod.GET)
    @ResponseBody
    public Map getWorkStatusPie(@PathVariable("tenant") String tenantKey,
                                @RequestParam(value = "day") String day,
                                @RequestParam(value = "deviceId") String deviceId) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List list = partnerChartsService.getWorkStatusPie(tenantId, day, deviceId);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
    }

    //获取：机床某天工作状态:通过代码计算
    @RequestMapping(value = "/partner/{tenant}/api/v1/workStatusPieByCalculate", method = RequestMethod.GET)
    @ResponseBody
    public Map getWorkStatusPieByCalculate(@PathVariable("tenant") String tenantKey,
                                @RequestParam(value = "day") String day,
                                @RequestParam(value = "deviceId") String deviceId) {
        try {
            String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
            List list = partnerChartsService.getWorkStatusPieByCalculate(tenantId, deviceId, day);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
        } catch (ParseException e) {
            e.printStackTrace();
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    //获取数据：某天idle原因的分布
    @RequestMapping(value = "/partner/{tenant}/api/v1/getDayIdleReason", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDayIdleReason(@PathVariable("tenant") String tenantKey,
                                                @RequestParam(value = "day") String day,
                                                @RequestParam(value = "deviceId") String deviceId) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String,Object>> list = partnerChartsService.getDayIdleReason(tenantId, day, deviceId);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/downTimeLine", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDownTimeLine(@PathVariable("tenant") String tenantKey,String deviceId,String time) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        try {
             Object sss = partnerChartsService.getDownTimeTable(tenantId,time,deviceId);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(sss).build();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    //
    @RequestMapping(value = "/partner/{tenant}/api/v1/getDownTimeAverageMTBRAndMTTRChart",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getDownTimeAverageMTBRAndMTTRChart(@PathVariable("tenant")String tenantKey,
                                                                 @RequestParam(value = "day")String day,
                                                                 @RequestParam(value = "deviceId")String deviceId,
                                                                 @RequestParam(value = "idleReason")String idleReason){
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        try{
            Map map = partnerChartsService.getDownTimeAverageMTBRAndMTTRChart(tenantId, day, deviceId, idleReason);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
        }catch (Exception e){
            System.out.print(e);
            return RestResponse.create(GlobalResponseCode.ERROR_Exception_ParseException).build();
        }
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/downTimeLineOfChart", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDownTimeLineOfChart(@PathVariable("tenant") String tenantKey,String deviceId,String time) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        try {
            Object sss = partnerChartsService.getTimePeriod(tenantId,time,deviceId);
            return RestResponse.create(GlobalResponseCode.SUCCESS).putData(sss).build();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/downTimePieOfChart", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDownTimePieChart(@PathVariable("tenant") String tenantKey,String deviceId,String endTime,String startTime) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        Map map =partnerChartsService.downTimeDataOfPie(tenantId,deviceId,endTime,startTime);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/downTimeBarOfChart", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDownTimeBarChart(@PathVariable("tenant") String tenantKey,String deviceId,String endTime,String startTime) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        Map map =partnerChartsService.downTimeDataOfBar(tenantId,deviceId,endTime,startTime);
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(map).build();
    }

    @RequestMapping(value = "/partner/{tenant}/api/v1/chartOfTypeOthers", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getPersonPositionChart(@PathVariable("tenant") String tenantKey,
                                                    @RequestBody Map<String,Object> map) {
        String tenantId = tenantService.getTenantByKey(tenantKey).getTenantId();
        List<Map<String,Object>> list =partnerChartsService.otherTimeLine(tenantId,map.get("deviceId").toString(),
                 map.get("day").toString(),(List<Map<String,Object>>)map.get("dataSource"));
        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(list).build();
    }
}
